﻿using System.IO.Ports;
using System.Text.Json.Serialization;

namespace Digitalmes.Infrastructure.IO.SerialPorts;

/// <summary>
/// 串口配置选项
/// </summary>
internal sealed class SerialPortOptions
{
    /// <summary>
    /// 串口名称，如 COM1、COM2 等。
    /// </summary>
    [NotNull]
    public string? PortName { get; init; }

    /// <summary>
    /// 波特率，默认 9600。
    /// </summary>
    public int BaudRate { get; init; } = 9600;

    /// <summary>
    /// 停止位，默认 One。
    /// </summary>
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public StopBits StopBits { get; init; } = StopBits.One;

    /// <summary>
    /// 数据位，默认 8。
    /// </summary>
    public int DataBits { get; init; } = 8;

    /// <summary>
    /// 奇偶位，默认 None。
    /// </summary>
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public Parity Parity { get; init; } = Parity.None;

    public int? ReadBufferSize { get; init; }

    public int? WriteBufferSize { get; init; }

    public int? ReceivedBytesThreshold { get; init; }

    public int? ReadTimeout { get; init; }

    public int? WriteTimeout { get; init; }

    /// <summary>
    /// 用途
    /// </summary>
    public string? Purpose { get; set; }

    /// <summary>
    /// 是否允许初始化后自动开启串口
    /// </summary>
    public bool AllowAutoOpen { get; set; }
}
